package com.zhiyou100.service.impl;

import java.sql.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.zhiyou100.mapper.TeaMapper;
import com.zhiyou100.model.Tea;
import com.zhiyou100.service.TeaService;

@Service("teaService")
public class TeaServiceImpl implements TeaService {
	@Resource
	TeaMapper teaMapper;

	public List<Tea> queryAll() {
		// TODO Auto-generated method stub
		return teaMapper.queryAll();
	}

	public int add(Tea tea) {
		// TODO Auto-generated method stub
		// 将最大得index数加上1 就等于 当前id得值了
		tea.setIndex(teaMapper.findMaxIndex() + 1);
		return teaMapper.add(tea);
	}

	public int update(Tea tea) {
		// TODO Auto-generated method stub
		return teaMapper.update(tea);
	}

	public Tea findById(Integer id) {
		// TODO Auto-generated method stub
		return teaMapper.findById(id);
	}

	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return teaMapper.deleteById(id);
	}

	public List<Tea> findByName(String name) {
		// TODO Auto-generated method stub
		return teaMapper.findByName(name);
	}

	public List<Tea> findByTea(Tea tea) {
		if (tea.getSex().equals("on")) {
			return teaMapper.findByName(tea.getName());
		}
		return teaMapper.findByTea(tea);
	}

	public List<Tea> findByTime(String begin, String end) {
		Date begin1 = null;
		Date end1 = null;
		try {
			begin1 = Date.valueOf(begin);
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			end1 = Date.valueOf(end);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return teaMapper.findByTime(begin1, end1);
	}

	public List<Tea> findByAll(Tea tea, String begin, String end) {
		Date begin1 = null;
		Date end1 = null;
		try {
			begin1 = Date.valueOf(begin);
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			end1 = Date.valueOf(end);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (tea.getSex().equals("on")) {
			tea.setSex(null);
		}
		return teaMapper.findByAll(begin1, end1, tea.getName(), tea.getSex());
	}
	//事务开启注解
	@Transactional
	public void findUpIndex(Integer id) {
		// 拿到上一条的内容
		Tea tea = teaMapper.findUpIndex(id);
		// 当前id 内容
		Tea tea2 = teaMapper.findById(id);
		// 先拿到当前id的索引
		int index = tea2.getIndex();
		// 交换索引
		tea2.setIndex(tea.getIndex());
		// 交换
		tea.setIndex(index);
		teaMapper.update(tea2);
		//强制 代码回滚  
		//TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();  
		teaMapper.update(tea);
	}

	public void findDownIndex(Integer id) {
		// 拿到下一条的内容
		Tea tea = teaMapper.findDownIndex(id);
		// 当前id 内容
		Tea tea2 = teaMapper.findById(id);
		// 先拿到当前id的索引
		int index = tea2.getIndex();
		// 交换索引
		tea2.setIndex(tea.getIndex());
		// 交换
		tea.setIndex(index);
		teaMapper.update(tea2);
		teaMapper.update(tea);

	}

}
